package pl.edu.agh.iosr.gamblingzone.dao.ejb;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import pl.edu.agh.iosr.gamblingzone.dao.BettingSlipDAO;
import pl.edu.agh.iosr.gamblingzone.model.BettingSlip;
import pl.edu.agh.iosr.gamblingzone.model.User;

/**
 * The Class BettingSlipDAOBean.
 */
public class BettingSlipDAOBean extends GenericDAOBean<BettingSlip, Long> 
							implements BettingSlipDAO {

	@SuppressWarnings("unchecked")
	public List<BettingSlip> getBettingSlipsBetween(User user, Date dateBegin, Date dateEnd, int page) {
		
		EntityManager em = getEntityManager();
		Query q = em.createQuery("select b from BETTING_SLIPS b" +
				" where b.creationDate >= :startDate" +
				" and b.creationDate <= :endDate " +
				" and b.owner = :owner"+
				" order by b.creationDate asc")
				.setMaxResults(getPageSize())
				.setFirstResult(page*getPageSize())
				.setParameter("startDate", dateBegin)
				.setParameter("endDate", dateEnd)
				.setParameter("owner", user);
		
		List<BettingSlip> retList = q.getResultList();
		
		if (retList == null) {
			return new LinkedList<BettingSlip>();
		}
		
		return retList;
	}

	public int getPageCount(User user) {		
		return getPageCount(user, "owner");
	}

	public List<BettingSlip> findAll(User user, int page) {
		return findAll(user, "owner", page);
	}

}
